/*
 * Interpreter.h
 *
 *  Created on: 15.04.2012
 *      Author: Eugene Kornykhin
 */

#ifndef INTERPRETER_H_
#define INTERPRETER_H_

#include <map>
#include <stack>
#include <string>
#include <vector>
#include "Poliz.h"
#include "PolizPosition.h"
#include "PolizIterator.h"

class PElement;

template <typename Data>
class GarbageCollector
{
public:
	GarbageCollector();
	~GarbageCollector();
	GarbageCollector(const GarbageCollector<Data> &);

	void add(const Data *);
private:
	enum { CAPACITY = 1024 };

	size_t size;
	std::vector<const Data *> garbage;

	void operator = (const GarbageCollector<Data> &);
};

class Interpreter {

public:
	Interpreter();
	virtual ~Interpreter();

	void interpret(const Poliz &, std::map<std::string, int> vars);

	int getVarValue(std::string name) const;
	void setVarValue(std::string name, int value);

	void pushPolizElement(const PElement &);
	const PElement &popPolizElement();

private:
	std::map<std::string, int> vars;
	std::stack<const PElement*> runtime;
	GarbageCollector<PElement> gc;
	Poliz poliz;
};

#endif /* INTERPRETER_H_ */
